#include <iostream>
#include <fstream>
#include <vector>
#include "Spectrum.h"

void Spectrum::load(std::string fileName)
{
	this->fileName = fileName;

	fileName=fileName+".in";
	std::ifstream fin;
	fin.open(fileName.c_str());
	fin>>numberOfOligos;
	fin>>this->length;
	energy = 0;
	

	char temp;
	std::vector<char> loadedOligo;
	
	maxOligoLength = 0;
	set.resize(getNumberOfOligos());
	for(int i=0;i<getNumberOfOligos();i++)
	{
		
		do
		{
			fin>>temp;	
			if(temp < 90 && temp >= 65)
			{				
				loadedOligo.push_back(temp);
				
			}			
		} while(temp < 90 && temp >= 65);
		fin.seekg(-1, std::ios_base::cur);
		OLIGO convertedOligo;
		
		for(std::vector<char>::size_type it=0;it<loadedOligo.size();it++)
		{
			NUCLEOTIDE ld;
			switch((int)loadedOligo[it])
			{
			case 65: ld=A;break;
			case 67: ld=C;break;
			case 71: ld=G;break;
			case 84: ld=T;break;
			default: ld=NONE;					
			}
			convertedOligo.val.push_back(ld);			
		}
		
		fin>>convertedOligo.rep_zero;
		fin>>convertedOligo.rep_one;
		fin>>convertedOligo.rep_two;
		fin>>convertedOligo.rep_many;

		
		set[i] = convertedOligo;
		int tempEn = convertedOligo.GetBindingEnergy();
		energy = tempEn > energy ? tempEn: energy;


		maxOligoLength = maxOligoLength > convertedOligo.GetLength()? maxOligoLength: convertedOligo.GetLength();
		
		loadedOligo.clear();
	}


	fin.close();

	std::ifstream bin;
	bin.open((fileName+".out").c_str());
	int l=0;

	bin>>l;

	for(int i=0;i<l;i++)
	{
		char temp;
		bin>>temp;
		NUCLEOTIDE ld;
		switch(temp)
		{
		case 65: ld=A;break;
		case 67: ld=C;break;
		case 71: ld=G;break;
		case 84: ld=T;break;
		default: ld=NONE;					
		}
		answer.push_back(ld);
	}
	
	bin.close();
};

OLIGO Spectrum::getOligo(int number) 
{		
	return set[number];
}

float Spectrum::validate(std::vector<std::pair<int, NUCLEOTIDE> >  * chain)
{
	int correct = 0;

	for(int i = 0; i<chain->size();i++)
	{
		
		if(chain->at(i).second == answer[i])
			correct ++;
	}
	return (float)correct/answer.size();
}